import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/core/common/utils/palette_generator.dart';
import 'package:flutter_demo/ui/pages/bar/controller.dart';
import 'package:flutter_demo/ui/pages/bar/widgets/my_loading.dart';
import 'package:flutter_demo/ui/pages/comic/home/controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'dart:ui' as ui;

import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class Recommend extends StatefulWidget {
  const Recommend({super.key});

  @override
  State<Recommend> createState() => _RecommendState();
}

class _RecommendState extends State<Recommend> {
  int _current = 0;
  final CarouselController _carouselController = CarouselController();
  final _listScroll = ScrollController();
  final RefreshController _refreshController = RefreshController();
  final _homeController = Get.find<HomeController>();

  static const List<dynamic> _movies = [
    {
      'title': 'Black Widow',
      'image':
          'https://pics0.baidu.com/feed/838ba61ea8d3fd1f69c4d0a322ef171397ca5f82.jpeg?token=864c3f289b34d45e17039fcd8fd307a7',
      'description': 'Black Widow'
    },
    {
      'title': 'The Suicide Squad',
      'image':
          'https://img0.baidu.com/it/u=2932188870,235853835&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=950',
      'description': 'The Suicide Squad'
    },
    {
      'title': 'Godzilla Vs Kong',
      'image':
          'https://img.jianbihua.com/sites/default/files/styles/nochange/public/images/2019-02/20190227170143_981908.jpg',
      'description': 'Godzilla Vs Kong'
    }
  ];

  @override
  void initState() {
    print("initState 被调用");
    super.initState();
    _listScroll.addListener(() {
      _homeController.updateBar(_listScroll.position.pixels);
    });
  }

  @override
  void activate() {
    super.activate();
  }

  Future<PaletteGenerator> _calculation(dynamic d) async {
    return PaletteGenerator.fromImageProvider(NetworkImage(d['image']));
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeController>(
        id: "barChange",
        builder: (control) {
          return Stack(children: [
            FutureBuilder(
                future:
                    _calculation(_movies[_current]), // a Future<String> or null
                builder: (BuildContext context,
                    AsyncSnapshot<PaletteGenerator> snapshot) {
                  return control.ifTransparent.value
                      ? Container(
                          height: control.barHeight + 200,
                          width: MediaQuery.of(context).size.width,
                          decoration: BoxDecoration(
                              color: snapshot.data?.dominantColor?.color),
                        )
                      : const SizedBox.shrink();
                }),
            Positioned(
                top: control.barHeight,
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                child: SmartRefresher(
                    controller: _refreshController,
                    header: WaterDropHeader(
                      waterDropColor: Colors.green.shade700,
                      refresh: MyLoading(),
                      complete: Container(),
                      completeDuration: Duration.zero,
                    ),
                    onRefresh: () => _refreshController.refreshCompleted(),
                    child: ListView(controller: _listScroll, children: [
                      CarouselSlider(
                        options: CarouselOptions(
                          height: 200.0,
                          autoPlay: true,
                          viewportFraction: 1.0,
                          enlargeCenterPage: true,
                          onPageChanged: (index, reason) {
                            setState(() => {_current = index});
                          },
                        ),
                        carouselController: _carouselController,
                        items: _movies.map((movie) {
                          return Builder(builder: (BuildContext context) {
                            return Stack(
                              children: [
                                //图片
                                Positioned.fill(
                                    child: Image.network(movie['image'],
                                        fit: BoxFit.cover)),
                                //图片提示文字
                                Positioned(
                                  right: 0,
                                  bottom: 0,
                                  child: Container(
                                      height: 17.h,
                                      decoration: const BoxDecoration(
                                          color:
                                              Color.fromARGB(255, 201, 61, 45),
                                          borderRadius: BorderRadius.only(
                                              topLeft: Radius.circular(12))),
                                      padding: const EdgeInsets.fromLTRB(
                                          10, 0, 10, 0),
                                      child: Center(
                                        child: Text(
                                          "hello world",
                                          style: TextStyle(
                                              color: Colors.white,
                                              fontSize: 12.sp),
                                        ),
                                      )),
                                )
                              ],
                            );
                          });
                        }).toList(),
                      ),
                      Container(
                        alignment: Alignment.center,
                        height: 500,
                        child: Text("abc"),
                      )
                    ])))
          ]);
        });
  }
}
